package structuralPattern.composite.example02;


import java.util.ArrayList;

/**
 * 文件夹
 */
public class Directory extends Entry{

    private String name;
    private ArrayList<Entry> directory = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return this.name;
    }

    // 获取下面所有文件的大小总和;
    // 1、如果entry对象是file类型，则调用getSize(),获取文件大小;
    // 2、如果entry对象是directory类型，则需要继续调用getSize(),获取下面所有文件大小;
    @Override
    public int getSize() {
        int size = 0;

        for (Entry entry : directory) {
            size += entry.getSize();
        }
        return size;
    }

    @Override
    public Entry add(Entry entry) {
        directory.add(entry);
        return null;
    }

    @Override
    public void printList(String prefix) {
        System.out.println(prefix + "/" + this);
        for (Entry entry : directory) {
            entry.printList(prefix + "/" + this.name);
        }
    }
}
